#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
@desc: .

@Project: kuberCloud 
@File: utils.py
@Author: admin
@Date: 2023/8/23 10:00 
"""
from kuberCloudTest.common.constant import AsyncType
from kuberCloudTest.exception import KuberCloudException


def _getUrl(host, port, uri):
    """
    splicing processing URL
    :param host: ip
    :param port: port
    :param uri: uri
    :return:
    """
    return 'http://{0}:{1}{2}'.format(host, port, uri)


def _getLogAction(operationType: int, asyncType: int = 0):
    """
    get basic data for post request
    :param operationType: operation type
    :param asyncType: asynchronous: 1 ; synchronous: 0
    :return:
    """
    if asyncType not in (AsyncType.SYNC.value, AsyncType.ASYNC.value):
        raise KuberCloudException('params error', 'asyncType should be 0 or 1')
    return dict(logAction={'id': operationType, 'name': '', 'async': asyncType})


def _getOperationType(operationType: int, asyncType: int = 0):
    """
    get basic data for post request
    :param operationType: operation type
    :param asyncType: asynchronous: 1 ; synchronous: 0
    :return:
    """
    if asyncType not in (AsyncType.SYNC.value, AsyncType.ASYNC.value):
        raise KuberCloudException('params error', 'asyncType should be 0 or 1')
    logAction = _getLogAction(operationType, asyncType)
    logAction.update(dict(operationType=operationType))
    return logAction


def _getDetail(content, detailId, field):
    """
    get detail by xxxId from response
    :param content: result from 'json.loads(response.content.decode())'
    :param detailId: detail uuid
    :param field: field in sql
    :return:
    """
    obj = None
    total = content.get('total', 0)
    if total != 0:
        mirrors = list(filter(lambda row: row[field] == detailId, content['rows']))
        obj = mirrors[0] if len(mirrors) > 0 else obj
    if not obj:
        content['msg'] = 'Not found {} [{}]'.format(field, detailId)

    del content['total']
    del content['rows']
    content['obj'] = obj

    return content




